package view;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import model.view.CasillaView;
import controller.Maquina;
import controller.exception.MaquinaInstantiationException;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CargarRodillo2 extends JPanel {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JButton botonAceptar;
	private static CargarRodillo2 instancia;
	private int cantidadCasillas;
	private Set<CasillaView> casillasView;
	private Map<String,Integer> nombreFrutaCodigo;
	private int posicionCambianteH=90;
	private List<JComboBox> combos=new ArrayList<JComboBox>();
	
	
	private CargarRodillo2(int cantidadCasillas) {
		super();
		try {
			this.cantidadCasillas=cantidadCasillas;
			casillasView=Maquina.getInstancia().obtenerCasillas();
			createMap();
			initGUI();
		} catch (MaquinaInstantiationException e) {
		}
	}
	
	public static CargarRodillo2 getInstancia(int cantidadCasillas)
	{
		instancia = new CargarRodillo2(cantidadCasillas);
		return instancia;
	}
	
	private void createMap() {
		nombreFrutaCodigo=new HashMap<String,Integer>();
		for(CasillaView view:casillasView){
			nombreFrutaCodigo.put(view.toString(), view.getCodigo());
		}
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(423, 321));
			this.setLayout(null);
					
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Seleccione las Casillas que forman el rodillo");
				jLabel1.setBounds(12, 12, 315, 29);
			}

			for (int i=0;i<cantidadCasillas;i++){
				JLabel jLabel3 = new JLabel();
				jLabel3.setText("Posicion " +(i+1));
				jLabel3.setBounds(18, posicionCambianteH, 73, 23);
				ComboBoxModel jComboBoxModel = new DefaultComboBoxModel(Utility.crearOpcionesCasilla(casillasView));
				JComboBox jComboBox = new JComboBox();
				jComboBox.setModel(jComboBoxModel);
				jComboBox.setBounds(275, posicionCambianteH, 200, 23);
				combos.add(jComboBox);
				this.add(jLabel3);
				this.add(jComboBox);
				posicionCambianteH=posicionCambianteH+30;
			}
			{
				jLabel2 = new JLabel();
				jLabel2.setText("");
				jLabel2.setBounds(18, posicionCambianteH+80, 73, 23);
				jLabel2.setVisible(false);
			}
			{
				botonAceptar = new JButton();
				this.add(botonAceptar);
				botonAceptar.setText("Agregar Rodillo");
				botonAceptar.setBounds(246, posicionCambianteH+80, 150, 35);
				botonAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt)
					{
						try {
							
								boolean result = Maquina.getInstancia()
										.crearRodillo(
												makeListCasillas(combos,
														nombreFrutaCodigo));
								if (result) {
									jLabel2.setText("Se ingreso correctamente el rodillo");
									jLabel2.setVisible(true);
									botonAceptar.setVisible(false);

								} else {
									jLabel2.setText("No se ingreso correctamente el rodillo, vuelva a intentarlo");
									jLabel2.setVisible(true);
								}
							
						} catch (MaquinaInstantiationException e) {
							System.out.println("La maquina ya fue creada");
						}
					}

					
				});
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected static List<Integer> makeListCasillas(List<JComboBox> comboBoxes, Map<String,Integer> nombreFrutaCodigo) {
		List<Integer> frutasId=new ArrayList<Integer>();
		for(int i=0;i<comboBoxes.size();i++){
			String nombreFruta=Utility.getSelectedValue(comboBoxes.get(i));
			frutasId.add(nombreFrutaCodigo.get(nombreFruta));
		}
		return frutasId;
	}


}
